package com.ys.nms.module.business.controller.admin.redData;

import com.ys.nms.framework.common.pojo.CommonResult;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.ys.nms.framework.common.pojo.CommonResult.success;

@RestController
@RequestMapping("/business/rrd")
@RequiredArgsConstructor
public class RrdMultiColumnController {

    private final RrdMultiColumnReader rrdMultiReader;
    private final IcmpPerfDataParser icmpParser;
    private final PortStatsDataParser portStatsParser;

    /**
     * 查询多列RRD数据
     */
    @GetMapping("/data/{deviceIp}")
    public ResponseEntity<RrdMultiQueryResult> getMultiColumnRrdData(
            @PathVariable String deviceIp,
            @RequestParam String rrdFile,
            @RequestParam(defaultValue = "now-1h") String startTime,
            @RequestParam(defaultValue = "now") String endTime) {

        RrdQueryParams params = new RrdQueryParams();
        params.setDeviceIp(deviceIp);
        params.setRrdFile(rrdFile);
        params.setStartTime(startTime);
        params.setEndTime(endTime);

        RrdMultiQueryResult result = rrdMultiReader.queryMultiColumnRrdData(params);

        if (result.getSuccess()) {
            return ResponseEntity.ok(result);
        } else {
            return ResponseEntity.badRequest().body(result);
        }
    }

    /**
     * 查询ICMP性能数据
     */
    @GetMapping("/icmp/{deviceIp}")
    public CommonResult<RrdMultiQueryResult> getIcmpData(
            @PathVariable String deviceIp,
            @RequestParam(defaultValue = "now-1h") String startTime,
            @RequestParam(defaultValue = "now") String endTime) {

        RrdQueryParams params = new RrdQueryParams();
        params.setDeviceIp(deviceIp);
        params.setRrdFile("icmp-perf.rrd");
        params.setStartTime(startTime);
        params.setEndTime(endTime);

        RrdMultiQueryResult result = icmpParser.parseIcmpData(params);

        if (result.getSuccess()) {
            // 添加ICMP特定统计信息
            Map<String, Object> stats = new HashMap<>();
//            stats.put("latencyStats", icmpParser.getIcmpLatencyStats(result));
//            stats.put("packetLossStats", icmpParser.getPacketLossStats(result));

            // 可以将统计信息添加到响应中
            result.setAdditionalInfo(stats);

            return success(result);
        } else {
            return success(result);
        }
    }

    /**
     * 查询端口统计数据
     */
    @GetMapping("/port-stats/{deviceIp}")
    public ResponseEntity<RrdMultiQueryResult> getPortStats(
            @PathVariable String deviceIp,
            @RequestParam String portId, // 端口ID，用于构建文件名
            @RequestParam(defaultValue = "now-1h") String startTime,
            @RequestParam(defaultValue = "now") String endTime) {

        RrdQueryParams params = new RrdQueryParams();
        params.setDeviceIp(deviceIp);
        params.setRrdFile("port-" + portId + ".rrd"); // 根据端口ID构建文件名
        params.setStartTime(startTime);
        params.setEndTime(endTime);

        RrdMultiQueryResult result = portStatsParser.parsePortStats(params);

        if (result.getSuccess()) {
            // 添加端口特定统计信息
            Map<String, Object> stats = new HashMap<>();
            stats.put("trafficStats", portStatsParser.getTrafficStats(result));
            stats.put("errorStats", portStatsParser.getErrorStats(result));

            result.setAdditionalInfo(stats);

            return ResponseEntity.ok(result);
        } else {
            return ResponseEntity.badRequest().body(result);
        }
    }

    /**
     * 获取指定列的有效数据
     */
    @GetMapping("/data/{deviceIp}/column/{columnName}")
    public ResponseEntity<Map<String, Object>> getColumnData(
            @PathVariable String deviceIp,
            @PathVariable String columnName,
            @RequestParam String rrdFile,
            @RequestParam(defaultValue = "now-1h") String startTime,
            @RequestParam(defaultValue = "now") String endTime) {

        RrdQueryParams params = new RrdQueryParams();
        params.setDeviceIp(deviceIp);
        params.setRrdFile(rrdFile);
        params.setStartTime(startTime);
        params.setEndTime(endTime);

        RrdMultiQueryResult fullResult = rrdMultiReader.queryMultiColumnRrdData(params);

        if (!fullResult.getSuccess()) {
            return ResponseEntity.badRequest().body(Map.of(
                    "success", false,
                    "message", fullResult.getMessage()
            ));
        }

        List<RrdMultiDataPoint> columnData = rrdMultiReader.getValidDataForColumn(fullResult, columnName);
//        Map<String, Object> columnStats = rrdMultiReader.calculateColumnStats(fullResult, columnName);

        return ResponseEntity.ok(Map.of(
                "success", true,
                "deviceIp", deviceIp,
                "rrdFile", rrdFile,
                "columnName", columnName,
                "dataPoints", columnData,
//                "stats", columnStats,
                "timeRange", startTime + " to " + endTime
        ));
    }
}